<?
function m_viewpages($ttrow,$n,$page,$link){
 global $link;
 $total = ceil($ttrow/$n);
 if ($total <= 1) return '';
 $v_f = 3;
 $v_a = 2;
 $v_l = 3;
 $max_pages = $v_f + $v_a + $v_l + 5;
 $z_1 = $z_2 = $z_3 = false;
 $link = preg_replace("#&pg=([0-9]{1,})#si","",$link);
 $return="<div id='page'>";
 $return.=" ";
 $pg = ($page)?$page:1;
 $pgt = $pg-1;
 $web_type	 = (m_get_config('url_type') == 0)?'':'.html';
 if ($pg != 1) { 
  $return .= "<a class=pagelink href='".$link."1".$web_type."' title ='Xem trang đầu'>&laquo;&laquo;</a> <a class=pagelink href=$link$pgt$web_type onfocus=this.blur() title='Xem trang trước'>&laquo;</a> "; 
 }
 elseif ($pg = 1) { 
  $return .= "<a class=pagelink href=# title ='Xem trang đầu'>&laquo;&laquo;</a> "; 
 } 
 for($m = 1; $m <= $total; $m++) {
 if ($total > $max_pages) {
 if (($m > $v_f) && (($m < $pg - $v_a) || ($m > $pg + $v_a)) && ($m < $total - $v_l + 1)) {
 if (!$z_1 && ($m > $v_f)) {
     $return .= "...";
     $z_1 = true;
    }
    elseif (!$z_2 && ($m > $pg + $v_a)) {
     $return .= "...";
     $z_2 = true;
    }
    continue;
   }
  }
  if($m == $pg) $return .= "<a class=\"pagecurrent\">$m</a> ";
  else {
   $return .= " <a href=\"$link$m$web_type\" title='Xem trang $m' class=\"pagelink\">$m</a> ";
   }
 }
 $pgs = $pg + 1;
 if ($pg != $total){ 
  $return .= "<a class='pagelink' href='$link$pgs$web_type' title='Xem trang kế tiếp'>&raquo;</a> <a class='pagelink' href='$link$total$web_type'  title='Xem trang cuối'>&raquo;&raquo;</a> "; 
  } 
 elseif ($pg = $total){ 
  $return .= "<a class='pagelink' href='#' title='Xem trang cuối'>&raquo;&raquo;</a>"; 
  }
 $return.="</div>";
 return $return;
} 
?>